/**
*      author: Y_Yao
*      created: 2024-09-23 21:16:15
**/

#include<bits/stdc++.h>

using namespace std;
using LL = long long;
const int INF = 0x3f3f3f3f;
int retl,retr,rets;
int sx,sy,ex,ey;
int dxl[4] = {0,-1,0,1};
int dyl[4] = {-1,0,1,0};
int dxr[4] = {0,-1,0,1};
int dyr[4] = {1,0,-1,0};
bool flagl,flagr;
//北 东 南 西
//0  1  2  3

void bfs(int n,int m,vector<vector<char>>& nums)
{
    vector<vector<bool>> vis(n,vector<bool>(m,false));
    queue<pair<int,int>> q;
    q.push({sx,sy});
    vis[sx][sy] = true;

    while(!q.empty())
    {
        rets++;
        int sz = q.size();
        for(int i = 0;i < sz;i++)
        {
            auto [a,b] = q.front();
            q.pop();


            for(int j = 0;j < 4;j++)
            {
                int x = a + dxl[j],y = b + dyl[j];
                if(x < 0 || x >= n || y < 0 || y >= m || vis[x][y] || nums[x][y] == '#') continue;
                if(x == ex && y == ey)
                {
                    rets++;
                    return;
                }
                q.push({x,y});
                vis[x][y] = true;
            }
        }
    }
}

void dfs_l(int sr,int sc,vector<vector<char>>& nums,int step,int face)
{
    int n = nums.size(),m = nums[0].size();

    if(sr == ex && sc == ey)
    {
        retl = step;
        flagl = true;
        return;
    }

    for(int i = 0;i < 4;i++)
    {
        if(flagl)break;
        int x = sr + dxl[(i + face) % 4],y = sc + dyl[(i + face) % 4];
        if(x < 0 || x >= n || y < 0 || y >= m || nums[x][y] == '#') continue;
        dfs_l(x,y,nums,step+1, abs(face + i + 3) % 4);
    }
}

void dfs_r(int sr,int sc,vector<vector<char>>& nums,int step,int face)
{
    int n = nums.size(),m = nums[0].size();

    if(sr == ex && sc == ey)
    {
        retr = step;
        flagr = true;
        return;
    }

    for(int i = 0;i < 4;i++)
    {
        if(flagr)break;
        int x = sr + dxr[(i + face) % 4],y = sc + dyr[(i + face) % 4];
        if(x < 0 || x >= n || y < 0 || y >= m || nums[x][y] == '#') continue;
        dfs_r(x,y,nums,step+1, abs(face + i + 3) % 4);
    }
}

void solve() 
{
    int n,m;
    cin >> m >> n;
    vector<vector<char>> nums(n,vector<char>(m));
    for(int i = 0;i < n;i++)
    {
        for(int j = 0;j < m;j++)
        {
            cin >> nums[i][j];
        }
    }

    for(int i = 0;i < n;i++)
    {
        for(int j = 0;j < m;j++)
        {
            if(nums[i][j] == 'S')
            {
                sx = i;
                sy = j;
            }
            else if(nums[i][j] == 'E')
            {
                ex = i;
                ey = j;
            }
        }
    }
    int facel,facer;

    if(sx == 0) facel = facer = 2;
    else if(sx == n-1) facel = facer = 0;
    else if(sy == 0) facel = 3,facer = 1;
    else facel = 1,facer = 3;

    bfs(n,m,nums);
    dfs_l(sx,sy,nums,1,facel);
    dfs_r(sx,sy,nums,1,facer);

    cout << retl << " " << retr << " " << rets << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
        rets = retl = retr = 0; 
        flagl = flagr = false;
    }
    return 0;
}